package com.ets.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;

/*******************************************************************************
 Utility methods for GUI display output
 <br><br>
 @author D.Merkling - 1:33:38 PM - Aug 22, 2011
 <br><br>
 @version 1.0  
*******************************************************************************/
public abstract class OutputUtils
  {
   public static JFrame setFrame (String title, int percent, int quadrant)
	 {
	  // Create method variables
	  int    sw  =   0;
	  int    sh  =   0;
	  int    fw  =   0;
	  int    fh  =   0;
	  int    x   =   0;
	  int    y   =   0;
	  double tmp = 0.0;
	   
	  // Create default frame
	  JFrame frame = new JFrame();

	  // Get the size of the screen
      Toolkit   tk  = Toolkit.getDefaultToolkit();
	  Dimension dim = tk.getScreenSize();
	  sw            = dim.getSize().width;
	  sh            = dim.getSize().height;

	  // Determine percentage of screen to use
	  if (percent < 1)
	    {
		 percent = 1;  
	    }
	  if (percent > 100)
	    {
		 percent = 100;  
	    }
	  double prcnt = percent / 100;
	  
	  // Calculate actual size of frame
	  tmp = sw / prcnt;
	  fw  = (int) tmp;
	  tmp = sh / prcnt;
	  fh  = (int) tmp;

	  // Determine the location of the frame
	  if (quadrant < 0)
	    {
		 quadrant = 0;  
	    }
	  else
	    {
		 if (quadrant > 4)
		 {
	      quadrant = 0;		 
		 }
	    }
	  switch (quadrant)
	    {
	     case 0:               // Centered
	       tmp = (sw - fw) / prcnt;
	       x   = (int) tmp;
	       tmp = (sh - fh) / prcnt;
	       y   = (int) tmp;
	       break;
	     case 1:               // Upper left
	       x = 0;
	       y = 0;
	       break;
	     case 2:               // Upper right
	       x = sw - fw;
	       y = 0;
	       break;
	     case 3:               // Lower left
	       x = 0;
	       y = sh - fh;
	       break;
	     case 4: 	           // Lower right
	       x = sw - fw;
	       y = sh = fh;
	       break;
	    }
	  
	  // Now, set all the frame attributes
	  frame.setVisible(true);
      frame.setTitle(title);
      frame.setSize(fw, fh);
      frame.setResizable(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocation(x, y);
      
      // Return the constructed frame
      return frame;
	 }

   public static JFrame setFrame (String title)
	 {
	  // Create default frame
	  JFrame frame = new JFrame();

	  // Set the default frame
	  frame = setFrame(title, 50, 0);
    
      // Return the constructed frame
      return frame;
	 }

   public static void setFrameSize (Window w, int prcnt)
	 {
	  Double tmpPrcnt = 100.0;
	  if ((prcnt > 0) && (prcnt < 100))
	    {
		 tmpPrcnt = prcnt * 1.0; 
	    }
	  tmpPrcnt     = tmpPrcnt / 100.0;
	  Toolkit   tk        = Toolkit.getDefaultToolkit();
	  Dimension d         = tk.getScreenSize();
	  int       ww = d.width;
	  int       wh = d.height;
	  Double    xw = ww * tmpPrcnt;
	  Double    xh = wh * tmpPrcnt;
	  w.setSize(xw.intValue(), xh.intValue());
	 }

   public static void centerWindow (Window w)
	 {
	  Toolkit   tk = Toolkit.getDefaultToolkit();
	  Dimension d  = tk.getScreenSize();
	  int       ww = (d.width  - w.getWidth())  / 2;
	  int       wh = (d.height - w.getHeight()) / 2;
	  w.setLocation(ww, wh);
	 }
  }
